function getClassNames(classStr, tagName) {
    if (document.getElementsByClassName) {
        return document.getElementsByClassName(classStr)
    } else {
        var nodes = document.getElementsByTagName(tagName), ret = [];
        for (i = 0; i < nodes.length; i++) {
            if (hasClass(nodes[i], classStr)) {
                ret.push(nodes[i])
            }
        }
        return ret;
    }
}
function hasClass(tagStr, classStr) {
    var arr = tagStr.className.split(/\s+/);  //这个正则表达式是因为class可以有多个,判断是否包含  
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == classStr) {
            return true;
        }
    }
    return false;
}
function iELowVersion() {
    var DEFAULT_VERSION = "9.0";
    var ua = navigator.userAgent.toLowerCase();
    var isIE = ua.indexOf("msie") > -1;
    var safariVersion;
    if (isIE) {
        safariVersion = ua.match(/msie ([\d.]+)/)[1]; //获取浏览器版本号
    }
    
    if (safariVersion * 1 < DEFAULT_VERSION * 1) { //若版本号低于IE9
        return false;
    }
    return true;
}
/*
if (!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/^\s*|\s*$/g, "");
        }
}
*/

function showPg() {
    var totals = 0;
    var p = 0;
    var k1 = 0;
    
    if (!iELowVersion()) {
        alert("IE浏览器版本过低，请升级ie9或以上！返回失败！");
        return;
    }
    
    var title = getClassNames("title", "div")[0].innerHTML;
    title = title.replace("<h3>", "").replace("</h3>", "").trim();
    
    var test;
    if (window.XMLHttpRequest) {
        test = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        test = new window.ActiveXObject();
    } else {
        alert("请升级至最新版本的浏览器");
    }
    if (test != null) {
        test.open("GET", "../searchdata/bloglists.json", true);
        test.send(null);
        test.onreadystatechange = function() {
            if (test.readyState == 4 && test.status == 200) {
                var obj = JSON.parse(test.responseText);
                totals = obj.bloglinks.length;
                let newbloglinks=obj.bloglinks.reverse()
                for (var k in newbloglinks) {
                    if (newbloglinks[k].title == title) {
                        k1 = parseInt(k) + 1;
                        p = thePages(k1, 5);

                        location.href = "../bloglist.html?page=" + p;
                    }
                }

            }
        };

    }
}
function thePages(totalcs,pagesize){
      //第几页
      var  thePage=1;
      if (totalcs / pagesize > parseInt(totalcs / pagesize)) {
        thePage = parseInt(totalcs / pagesize) + 1;
    } else {
        thePage = parseInt(totalcs / pagesize);
    }
    return thePage;
}

window.onscroll = function() {scrollFunction()};
     
function scrollFunction() {
    if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
        document.getElementById("backtop").style.display = "block";
    } else {
        document.getElementById("backtop").style.display = "none";
    }
}
 
// 点击按钮，返回顶部
function topFunction() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
}
